package com.lu.manage.modular.system.controller;

import com.lu.manage.core.base.controller.BaseController;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.modular.system.model.Datasource;
import com.lu.manage.modular.system.service.DatasourceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Project sKnowledge_Blog
 * @Author: zhanglu
 * @Date: 2019-07-14 16:06:25
 * @Description: 数据源 控制器
 */
@Controller
@RequestMapping("/system/datasource")
public class DatasourceController extends BaseController {

    private static final String PREFIX = "/modular/system/datasource";

    @Autowired
    private DatasourceService datasourceService;

    @GetMapping("")
    public String index(){
        return PREFIX + "/datasource.html";
    }

    /**
     * 跳转到添加数据源列表
     */
    @GetMapping("/toAdd")
    public String toAdd() {
        return PREFIX + "/datasource_add.html";
    }

    /**
     * 跳转到修改数据源列表
     */
    @GetMapping("/toUpdate")
    public String toUpdate(Long id, Model model) {
        Datasource obj = datasourceService.getById(id);
        model.addAttribute("obj", obj);
        return PREFIX + "/datasource_update.html";
    }

    /**
     * 分页查询
     */
    @GetMapping("/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> map) {
        return datasourceService.listPage(map);
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions(value = "/system/datasource/add")
    public Object add(Datasource datasource){
        datasourceService.add(datasource);
        return new SuccessTip();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions(value = "/system/datasource/update")
    public Object update(Datasource datasource){
        datasourceService.update(datasource);
        return new SuccessTip();
    }

    /**
     * 删除&批量
     */
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = "/system/datasource/delete")
    public Object delete(String data){
        datasourceService.delete(data);
        return new SuccessTip();
    }

}
